package com.erp.app.admin.companySetup.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.app.admin.companySetup.criteria.CompanyImageSrchCriteria;
import com.erp.app.admin.companySetup.pk.CompanyImagePk;
import com.erp.app.admin.companySetup.vo.CompanyImageVo;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.criteria.SimpleBaseSrchCriteria;
import com.erp.framework.model.dao.SimplePageEditStyleDao;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;

public class CompanyImageDao extends SimplePageEditStyleDao {

    public ChildList selectByParent(SimpleBaseVo parent, Connection conn) throws FindRecordException {
        return selectByParent(parent, CompanyImageVo.class, conn);
    }

    protected String sqlQuery(SimpleBaseSrchCriteria crit) {

        CompanyImageSrchCriteria criteria = (CompanyImageSrchCriteria) crit;

        StringBuffer sb = new StringBuffer();
        sb.append("SELECT COMPANY_CODE, ID, CONTENT_TYPE, IMAGE, CREATE_USER, CREATE_DATE");
        sb.append(", UPDATE_USER, UPDATE_DATE ");
        sb.append(" FROM T_COMPANY_IMAGE ");
        sb.append(toWhereClause(criteria));
        sb.append(" ORDER BY COMPANY_CODE ");
        return sb.toString();
    }

    private String toWhereClause(CompanyImageSrchCriteria criteria) {
        StringBuffer whereClause = new StringBuffer();

        if (criteria.getSrchCompanyCode() != null) {
            whereClause.append(whereClause.length() == 0 ? "WHERE " : "AND ");
            whereClause.append("UPPER(COMPANY_CODE) = '" + criteria.getSrchCompanyCode().trim().toUpperCase() + "' ");
        }

        if (criteria.getSrchId() != null) {
            whereClause.append(whereClause.length() == 0 ? "WHERE " : "AND ");
            whereClause.append("UPPER(ID) = '" + criteria.getSrchId().trim().toUpperCase() + "'");
        }

        return whereClause.toString();
    }

    protected String sqlInsert() {
        return "INSERT INTO T_COMPANY_IMAGE (" + "COMPANY_CODE, ID, CONTENT_TYPE, IMAGE, CREATE_USER, CREATE_DATE"
                + ", UPDATE_USER, UPDATE_DATE) " + "VALUES (?,?,?,?,?,sysdate,?,?)";
    }

    protected void setValuesForInsert(PreparedStatement pStmt, Object obj) throws SQLException {
        CompanyImageVo companyImageVO = (CompanyImageVo) obj;
        int i = 1;
        pStmt.setString(i++, companyImageVO.getCompanyCode());
        pStmt.setString(i++, companyImageVO.getId());
        setNullableString(pStmt, i++, companyImageVO.getContentType());
        setNullableBytes(pStmt, i++, companyImageVO.getImage());
        setNullableString(pStmt, i++, companyImageVO.getCreateUser());
        setNullableString(pStmt, i++, companyImageVO.getUpdateUser());
        setNullableTimestamp(pStmt, i++, companyImageVO.getUpdateDate());
    }

    protected String sqlUpdate() {
        return "UPDATE T_COMPANY_IMAGE SET " + "CONTENT_TYPE=?, IMAGE=?, UPDATE_USER=?, UPDATE_DATE=sysdate "
                + "WHERE COMPANY_CODE = ? " + "AND ID = ? " + "AND (UPDATE_DATE IS NULL OR UPDATE_DATE = ?) ";
    }

    protected void setValuesForModify(PreparedStatement pStmt, Object obj) throws SQLException {
    	CompanyImageVo companyImageVO = (CompanyImageVo) obj;
        int i = 1;
        setNullableString(pStmt, i++, companyImageVO.getContentType());
        setNullableBytes(pStmt, i++, companyImageVO.getImage());
        setNullableString(pStmt, i++, companyImageVO.getUpdateUser());
        pStmt.setString(i++, companyImageVO.getCompanyCode());
        pStmt.setString(i++, companyImageVO.getId());
        setNullableTimestamp(pStmt, i++, companyImageVO.getUpdateDate());
    }

    protected String sqlDelete() {
        return "DELETE FROM T_COMPANY_IMAGE" + " WHERE COMPANY_CODE = ? " + " AND ID = ? ";
    }

    protected void setValuesForDelete(PreparedStatement pStmt, Object obj) throws SQLException {
    	CompanyImageVo companyImageVO = (CompanyImageVo) obj;
        int i = 1;
        pStmt.setString(i++, companyImageVO.getCompanyCode());
        pStmt.setString(i++, companyImageVO.getId());
    }

    protected String sqlFind() {
        return "SELECT COMPANY_CODE, ID, CONTENT_TYPE, IMAGE, CREATE_USER, CREATE_DATE" + ", UPDATE_USER, UPDATE_DATE "
                + "FROM T_COMPANY_IMAGE " + "WHERE COMPANY_CODE = ? " + "AND ID = ? ";

    }

    protected void setValuesForFind(PreparedStatement pStmt, Object obj) throws SQLException {
        CompanyImagePk pk = (CompanyImagePk) obj;
        int i = 1;
        pStmt.setString(i++, pk.getCompanyCode());
        pStmt.setString(i++, pk.getId());
    }

    protected String sqlList() {
        StringBuffer sb = new StringBuffer("SELECT COMPANY_CODE, ID, CONTENT_TYPE, IMAGE, CREATE_USER, CREATE_DATE");
        sb.append(", UPDATE_USER, UPDATE_DATE ");
        sb.append("FROM T_COMPANY_IMAGE ");
        sb.append("WHERE COMPANY_CODE = ? ");

        return sb.toString();
    }

    protected void setValuesForList(PreparedStatement pStmt, Object obj) throws SQLException {
    	CompanyImagePk pk = (CompanyImagePk) obj;
        int i = 1;
        pStmt.setString(i++, pk.getCompanyCode());
    }

    protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {
        CompanyImageVo companyImageVO = new CompanyImageVo();
        companyImageVO.setCompanyCode(rs.getString("COMPANY_CODE"));
        companyImageVO.setId(rs.getString("ID"));
        companyImageVO.setContentType(rs.getString("CONTENT_TYPE"));
        companyImageVO.setImage(rs.getBytes("IMAGE"));
        companyImageVO.setCreateUser(rs.getString("CREATE_USER"));
        companyImageVO.setCreateDate(rs.getTimestamp("CREATE_DATE"));
        companyImageVO.setUpdateUser(rs.getString("UPDATE_USER"));
        companyImageVO.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));
        return companyImageVO;
    }

}
